package com.pmsystem.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.User;
import com.pmsystem.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	
	/**
	 * <p>Title: login</p >  
	 * <p>Description: 业主登陆</p >  
	 * @author 泽强
	 * @param request
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value="user/login",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> login(HttpServletRequest request, String username, String password) {
		User user = userService.login(username, password);
		String msg = null;
		if (user != null) {
			Byte enable = user.getEnable();
			if (enable == 1) {
				request.getSession().setAttribute("user", user);
				msg = "success" ;
				
			}else {
				msg = "账号不可用";
			}
		} else {
			msg = "账号或密码错误";
		}
		Map<String, String> map = new HashMap<>();
		map.put("msg", msg);
		return map;
	}
	
	@RequestMapping(value="user/logout",method=RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		request.getSession().invalidate();
		return "redirect:userLogin.html";
	}
	
	
	/**
	 * 
	 * <p>Title: home</p >  
	 * <p>Description: </p >  
	 * @author 泽强
	 * @param request
	 * @return
	 */
	@RequestMapping(value="user/home",method=RequestMethod.GET)
	public String home(HttpServletRequest request) {
		
		return "user/index";
	}
	
	
	
	
	/**
	 * <p>Title: update</p >  
	 * <p>Description: 业主更改信息</p >  
	 * @author 泽强
	 * @param user
	 * @return
	 */
	@RequestMapping(value="user/update",method=RequestMethod.POST)
	@ResponseBody
	public Object update(HttpServletRequest request,User user ) {
		String msg = null;
		System.out.println(user);
		boolean flag = userService.update(user);
		if(flag) {
			msg = "success";
			user = userService.getByUserId(user.getUserId());
			request.getSession().setAttribute("user", user);
		}else {
			msg = "error";
		}
		Map<String, String> map = new HashMap<>();
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: getOwnInfo</p >  
	 * <p>Description: 业主获得自己的信息</p >  
	 * @author 泽强
	 * @param request
	 * @return
	 */
	@RequestMapping(value="user/getOwnInfo",method=RequestMethod.GET)
	@ResponseBody
	public Object getOwnInfo(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		return user;
	}
	
	/**
	 * 
	 * <p>Title: modifyPassword</p >  
	 * <p>Description: 修改密码</p >  
	 * @author 泽强
	 * @param request
	 * @return
	 */
	@RequestMapping(value="user/modifyPassword",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> modifyPassword(HttpServletRequest request,String oldPassword,String newPassword) {
		User user = (User) request.getSession().getAttribute("user");
		Map<String, String> map = new HashMap<>();
		if(user==null) {
			map.put("msg", "未登录");
			map.put("staut", "error");
		}else {
			 map = userService.modifyPassword(user, oldPassword, newPassword);
		}
		return map;
	}
	
	
	/**
	 * <p>Title: regist</p >  
	 * <p>Description:业主注册</p >  
	 * @author 魏家洛
	 * @param user
	 * @return
	 */
	@RequestMapping(value="user/regist",method=RequestMethod.POST)
	@ResponseBody
	public boolean regist(User user) {
		System.out.println(user);
		return userService.regist(user);
	}
}
